package com.jy.api.service.impl;

import com.jy.api.dao.NumDao;
import com.jy.api.dao.NumTransactionDao;
import com.jy.api.dao.OrderDao;
import com.jy.api.dao.PartitionDao;
import com.jy.api.domain.*;
import com.jy.api.service.PartitionService;
import com.jy.api.signUpReq.admin.PartitionReq;
import com.jy.api.signUpReq.pos.IndexParAndNumAllRes;
import com.jy.api.signupRes.StorePartRes;
import com.jy.api.util.Result;
import com.jy.api.util.TimeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by xiaohua on 2019/2/18.
 */
@Service
public class PartitionServiceImpl implements PartitionService {
    @Autowired
    private PartitionDao partitionDao;
    @Autowired
    private NumDao numDao;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private NumTransactionDao transactionDao;


    @Override
    public List<Partition> getAllPar(String storeId) {
        List<Partition> partition = partitionDao.findAllByStoreIdAndEnabledOrderBySortDesc(storeId, true);
        return partition;
    }

    @Override
    @Transactional
    public Result addPar(PartitionReq req) {
        List<Partition> partition = partitionDao.findAllByStoreIdAndEnabledOrderBySortDesc(req.getStoreId(), true);
        for (Partition p : partition) {
            if (StringUtils.equals(p.getPartitionName(), req.getPartitionName())) {
                return new Result(400, "该分区名字已存在，请核实后进行添加");
            }
        }
        Partition part = new Partition(req);
        partitionDao.save(part);
        return new Result(200, "添加分区成功");
    }

    @Override
    @Transactional
    public Result deleteParById(PartitionReq req) {
        Partition partition = partitionDao.findOne(req.getId());
        List<Num> nums = numDao.findAllByPartitionIdAndEnabledOrderBySortDesc(req.getId(), true);
        if (!nums.isEmpty()) {
            return new Result(400, "该分区下还有台桌，请禁用台桌在进行删除操作。");
        } else {
            partition.setEnabled(false);
            partitionDao.save(partition);
            return new Result(200, "该分区已成功删除！");
        }
    }

    @Override
    @Transactional
    public Result updateParById(PartitionReq req) {
        Partition par = partitionDao.findOne(req.getId());
        List<Partition> partition = partitionDao.findAllByStoreIdAndEnabledOrderBySortDesc(req.getStoreId(), true);
        for (Partition p : partition) {
            if (StringUtils.equals(p.getPartitionName(), req.getPartitionName())) {
                return new Result(400, "该分区名字已存在，请核实后进行添加");
            }
        }
        if (null != req.getPartitionName()) {
            par.setPartitionName(req.getPartitionName());
        } else {
            par.setSort(req.getSort());
        }
        par.setRules(req.getRuleId());
        partitionDao.save(par);
        return new Result(200, "分区名字修改成功");
    }


    /**
     * 查找店铺下的所有分区以及桌号
     *
     * @param storeId
     * @return
     */
    @Override
    public IndexParAndNumAllRes getStorePart(String storeId, String source, int parId) {
        IndexParAndNumAllRes res = new IndexParAndNumAllRes();
        List<IndexParAndNumAllRes.Par> resPar = new ArrayList<>();
        List<IndexParAndNumAllRes.Par.Desk> desks = new ArrayList<>();
        List<Num> nums;
        if (StringUtils.equals(source, "ALL")) {
            if (parId == 0) {
                nums = numDao.findAllByStoreIdAndEnabledOrderBySortDesc(storeId, true);
            } else {
                nums = numDao.findAllByPartitionIdAndEnabledOrderBySortDesc(parId, true);
            }
        } else if (StringUtils.equals(source, "BUSY")) {
            if (parId == 0) {
                nums = numDao.findAllByStoreIdAndEnabledAndStatusOrderBySortDesc(storeId, true, NumStatus.BUSY);
            } else {
                nums = numDao.findAllByPartitionIdAndStatusAndEnabledOrderBySortDesc(parId, NumStatus.BUSY, true);
            }
        } else if (StringUtils.equals(source, "BOOK")) {
            if (parId == 0) {
                nums = numDao.findAllByStoreIdAndEnabledAndStatusOrderBySortDesc(storeId, true, NumStatus.BOOK);
            } else {
                nums = numDao.findAllByPartitionIdAndStatusAndEnabledOrderBySortDesc(parId, NumStatus.BOOK, true);
            }
        } else {
            if (parId == 0) {
                nums = numDao.findAllByStoreIdAndEnabledAndStatusOrderBySortDesc(storeId, true, NumStatus.FREE);
            } else {
                nums = numDao.findAllByPartitionIdAndStatusAndEnabledOrderBySortDesc(parId, NumStatus.FREE, true);
            }
        }
        if (parId == 0) {
            List<Partition> list = partitionDao.findAllByStoreIdAndEnabledOrderBySortDesc(storeId, true);
            for (Partition p : list) {
                IndexParAndNumAllRes.Par par = new IndexParAndNumAllRes.Par();
                par.setParId(p.getId());
                par.setParName(p.getPartitionName());
                resPar.add(par);
            }
        } else {
            Partition partition = partitionDao.findOne(parId);
            IndexParAndNumAllRes.Par par = new IndexParAndNumAllRes.Par();
            par.setParId(partition.getId());
            par.setParName(partition.getPartitionName());
            resPar.add(par);
        }
        for (Num n : nums) {
            IndexParAndNumAllRes.Par.Desk desk = new IndexParAndNumAllRes.Par.Desk();
            Order order = orderDao.findByNumIdAndStatusFinish(n.getId(), false);
            NumTransaction transaction = transactionDao.findByNumIdAndStatus(n.getId(), false);
            NumTransaction nowTransaction = null;
            if (transaction != null) {
                int min = TimeUtils.getMinTimeDifference(transaction.getBeginTime().toString(), new Date().toString());
                int hour = min / 60;
                int minute = min % 60;
                transaction.setInTime(hour + "小时" + minute + "分钟");
                nowTransaction = transactionDao.save(transaction);
            }
            if (nowTransaction != null) {
                desk.setBookArriveTime(nowTransaction.getBookTime());
                desk.setBookName(nowTransaction.getBookName());
                desk.setPersonCount(nowTransaction.getPeople());
                desk.setBookedEnabled(nowTransaction.isBookStatus());
                desk.setInTime(nowTransaction.getInTime());
            }
            if (order != null) {
                desk.setBillEnabled(true);
                desk.setAmount(order.getAmount());
            }
            desk.setParId(n.getPartitionId());
            desk.setDeskID(n.getId());
            desk.setDeskName(n.getNumName());
            desks.add(desk);
            if (desk.isBookedEnabled()) {
                desk.setRunStatusName("预订中");
            } else {
                desk.setRunStatusName("空闲中");
            }
        }
        for (IndexParAndNumAllRes.Par par : resPar) {
            List<IndexParAndNumAllRes.Par.Desk> resultDesk = new ArrayList<>();
            for (IndexParAndNumAllRes.Par.Desk desk : desks) {
                if (par.getParId() == desk.getParId()) {
                    resultDesk.add(desk);
                }
            }
            par.setDesks(resultDesk);
        }
        res.setPars(resPar);
        return res;
    }


}
